package com.wave12.utility;

import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MyDrawUtil {
    /**
     *  引入自定义的字体
     * @param fontStyle 字体样式
     * @param fontSize  字体大小
     * @return
     */
    public static Font getFont(int fontStyle, float fontSize) {
        Font font = null;
        FileInputStream fileInputStream = null;
        String fontUrl = "";
        try {
            String rootPath = ResourceUtils.getURL("classpath:").getPath();
            switch (fontStyle) {
                case 1:
                    //文悦新青年体
                    fontUrl = rootPath + "static/res/youth.otf";
                    break;
                default:
                    fontUrl = rootPath + "static/res/simkai.ttf";
                    break;
            }
            fileInputStream = new FileInputStream(new File(fontUrl));
            Font tempFont = Font.createFont(Font.TRUETYPE_FONT,fileInputStream);
            font = tempFont.deriveFont(fontSize);
            GraphicsEnvironment ge = GraphicsEnvironment
                    .getLocalGraphicsEnvironment();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return font;
    }
    /**
     * 将字符串按照自定义的间隔写入
     * @param str   目标字符串
     * @param x     写入的位置的x轴
     * @param y     写入的位置的y轴
     * @param rate  写入间隔
     * @param g     画布
     * @param fontSize  字体的大小
     */
    public static void drawString(String str,int x,int y,int rate, Graphics2D g,int fontSize){
        String tempStr="";
        int tempx=x;
        int tempy=y;
        while (str.length()>0){
            tempStr=str.substring(0, 1);
            str=str.substring(1, str.length());
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(tempStr, tempx, tempy);
            tempx = tempx + fontSize - rate;
        }
    }

    public static void main(String[] args){
        try {
            String rootPath = ResourceUtils.getURL("classpath:").getPath();
            //创建画布
            BufferedImage image = new BufferedImage(280,35, BufferedImage.TYPE_INT_RGB);
            //背景图（使用网络链接获取）
            //BufferedImage background = ImageIO.read(new URL("http://.../background.png"));
            //头像（使用本地路径获取）
            // BufferedImage background = ImageIO.read(new File(rootPath + "static/images/bg.png"));
            //字体背景图
            //BufferedImage fontBd = ImageIO.read(new URL("http://.../fontBd.png"));
            //开启画图
            Graphics2D graphics = image.createGraphics();
            //把背景图添加到画布中
            //graphics.drawImage(background.getScaledInstance(280,35,Image.SCALE_DEFAULT),0,0,null);
            //graphics.setBackground(Color.decode("#35813F"));
            graphics.setColor(Color.decode("#35813F"));
            graphics.fillRect(0, 0, 280, 35);

            //写入标题，标题引入自定义字体
            Font font = getFont(1,20);
            graphics.setColor(Color.decode("#ffffff"));
            graphics.setFont(font);
            //drawString("论标题的重要性",12,25,3,graphics,20);//将字符串按照自定义的间隔写入，防止字体全部写在同一个地方
            //添加字体背景
            //graphics.drawImage(fontBd.getScaledInstance(doctorMessage.length()*17,35,Image.SCALE_DEFAULT),20,136,null);
            //写入信息
            graphics.setFont(new Font("楷体", Font.PLAIN, 20));
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); //去字体锐化

            graphics.setColor(Color.black);
            graphics.drawString("WEB前端技术", 13, 25);

            graphics.setColor(Color.decode("#ffffff"));
            graphics.drawString("WEB前端技术", 12, 24);



            //关闭画布创作
            graphics.dispose();
            //根据画布内容生成图片
            ImageIO.write(image, "png", new File(rootPath + "static/tmp/test.png"));
            System.out.println("图片生成完毕");
        } catch (Exception ex) {
            System.out.println("error: "+ex.getMessage());
            ex.printStackTrace();
        }

    }
}
